﻿using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Senparc.Weixin.MP.Entities;
using ZilLion.Service.Core.Domain.EntityBase;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface;
using ZilLion.Service.WeChatMall.Domain.Requests;
using ZilLion.Service.WeChatMall.Domain.Responses;
using ZilLion.Service.WeChatMall.Domain.Services.Interface;

namespace ZilLion.Service.WeChatMall.Controllers
{
    /// <summary>
    ///     微商城客户信息
    /// </summary>
    [Route("Api/WechatMall/Cust")]
    public class CustController : WechatMallControllerBase
    {
        private readonly ICustAddressRepository _custAddressRepository;
        private readonly IWechatMpProxyService _wechatMpProxyService; 

        private readonly ICustReponsitory _custReponsitory;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="custReponsitory"></param>
        /// <param name="custAddressRepository"></param>
        /// <param name="wechatMpProxyService"></param>
        public CustController(ICustReponsitory custReponsitory,
            ICustAddressRepository custAddressRepository, IWechatMpProxyService wechatMpProxyService)
        {
            _custReponsitory = custReponsitory;
            _custAddressRepository = custAddressRepository;
            _wechatMpProxyService = wechatMpProxyService;
        }
        /// <summary>
        ///获得客户信息
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetCustInfo")]
        [HttpGet]
        public CustResponse GetCustInfo(string token = null)
        {
            var custId = base.GetCustId();
            var custEntity = this._custReponsitory.GetCustEntity(custId);
            WeixinUserInfoResult userInfo = this._wechatMpProxyService.GetUserInfo(base.GetWechatToken(), base.GetOpenId());
            return new CustResponse { Cust = custEntity, WechatUserinfo = userInfo };

        }
        /// <summary>
        /// 保存客户信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("SaveCustAddress")]
        [HttpPost]
        public CustAddressResponse SaveCustAddress([FromBody]CustAddressRequest request, string token = null)
        {
            var entity = _custAddressRepository.SaveAddress(request.Address);
            return new CustAddressResponse {Address = entity};
        }

        /// <summary>
        /// 获得客户地址列表
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetCustAddress")]
        [HttpGet]
        public CustAddressListResponse GetCustAddress(string token = null)
        {
            var custid = GetCustId();
            var list = _custAddressRepository.GetAddressList(custid);
            return new CustAddressListResponse {AddressList = list.ToList()};
        }

        /// <summary>
        ///     获取默认地址
        /// </summary>
        /// <param name="adcode"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetDefaultAddress")]
        [HttpGet]
        public CustAddressResponse GetDefaultAddress(string adcode, string token = null)
        {
            var custid = GetCustId();
            var entity = _custAddressRepository.GetDefaultAddress(adcode, custid);
            return new CustAddressResponse {Address = entity};
        }

        /// <summary>
        ///     设置默认地址
        /// </summary>
        /// <param name="request"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("SetDefaultAddress")]
        [HttpPost]
        public CustAddressResponse SaveCustAddress([FromBody]SetDefaultAddressRequest request,
            string token = null)
        {
            var entity = _custAddressRepository.SetDefaultAddress(request.AddressId);
            return new CustAddressResponse {Address = entity};
        }
        /// <summary>
        /// 根据地址ID 获得客户地址
        /// </summary>
        /// <param name="addressid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetCustddressById")]
        [HttpGet]
        public CustAddressResponse GetCustddressById(long addressid, string token = null)
        {
            var entity = _custAddressRepository.GetAddress(addressid);
            return new CustAddressResponse {Address = entity};
        }

        /// <summary>
        /// 移除客户地址
        /// </summary>
        /// <param name="request"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("removeCustAddress")]
        [HttpPost]
        public ResponseBase RemoveCustAddress([FromBody]CustAddressRequest request, string token = null)
        {
            _custAddressRepository.RemoveAddress(request.Address);
            return new ResponseBase();
        }
    }
}